package com.wilson.integration.lease.admin.schedule;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wilson.integration.lease.admin.service.LeaseAgreementService;
import com.wilson.integration.lease.common.entity.LeaseAgreement;
import com.wilson.integration.lease.common.enums.LeaseStatus;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduledTasks {

    @Resource
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        leaseAgreementService.update(
                new LambdaUpdateWrapper<LeaseAgreement>()
                        .le(LeaseAgreement::getLeaseEndDate, new Date())
                        .set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED)
                        .in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING));
    }
}